package com.test.daily.leetcode.y2022.m05.day0530.v01;

import java.util.ArrayList;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/5/30 下午 10:38
 * @version: 1.0
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(0);
        root.right = new TreeNode(1);
        root.left.left = new TreeNode(0);
        root.left.right = new TreeNode(1);
        root.right.left = new TreeNode(0);
        root.right.right = new TreeNode(1);
        System.out.println(new TreeNode().sumRootToLeaf(root));

    }
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }


        public int sumRootToLeaf(TreeNode root) {
            return process(root, 0);
        }

        public int process(TreeNode root, int val) {
            if(root == null){
                return 0;
            }
            val = (val << 1) | root.val;
            if(root.left == null && root.right == null){
                return val;
            }
            return process(root.left, val) + process(root.right, val);
        }
    }
}
